package com.googlecode.bip.xml.export;

import java.io.IOException;

/**
 * Help to generate XML
 *
 * @author Claude CHOISNET
 */
public class XMLAppenderHelper
{
    private XMLAppenderHelper()
    {
        // all static
    }

    /**
     * TODO: Doc
     *
     * @param <T>
     * @param a
     * @param iterable
     * @throws IOException
     */
    public static <T extends XMLAppender> void appendXML( Appendable a, Iterable<T> iterable )
        throws IOException
    {
        for( T t : iterable ) {
            t.appendXML( a );
            }
    }

    /**
     * TODO: Doc
     *
     * @param a
     * @param entityName
     * @param value
     * @throws IOException
     */
    public static void appendXMLValue( Appendable a, String entityName, String value )
        throws IOException
    {
        a.append( "<"  ).append( entityName ).append( ">" )
         .append( encodeToXMLText( value ) )
         .append( "</"  ).append( entityName  ).append( ">\n" );
    }

    /**
     * TODO: Doc
     *
     * @param a
     * @param entityName
     * @param value
     * @throws IOException
     */
    public static void appendXMLValue( Appendable a,String entityName, int value )
        throws IOException
    {
        appendXMLValue( a, entityName, Integer.toString( value ) );
    }

    /**
     * TODO: Doc
     *
     * @param value
     * @return
     */
    public static String encodeToXMLText( final String value )
    {
        return value.replace( "<", "&lt;" ).replace( ">", "&gt;" );
    }
}
